<?php 
/**
* Base class to define a singleton-like behaviour.
* Any descendant of the Singleton class have a singleton behaviour: it can instantiated only once
* per execution. 
* The isntance for any existing Singleton object can be recovered as follow:
* <code>
* Singleton::GetInstance( class_name )
* </code>
*@package Core
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
class BaseClassSingleton
{
	static private $singleton = array();
	/**
	* The process of registering and setting up a Singleton.
	*<p>This is here because it needs to be performed both at crerating time and at load time
	*after an unserialize.</p>
	*/
	protected function __SetupSingleton()
	{
		$class = get_class($this);
		if ( isset( BaseClassSingleton::$singleton[$class] ) )
			throw new Exception("Class '".$class."' is a singleton and has already been initialized!");
		BaseClassSingleton::$singleton[$class] = $this;
	}
	/**
	* Construct the Singleton (check for duplicates).
	*/
	protected function __construct()
	{
		$this->__SetupSingleton();
	}
	/**
	* Wake up from serialization, checks for duplicates.
	*/
	public function __wakeup()
	{
		$this->__SetupSingleton();
	}
	/**
	* Static method to get a specific Singleton given its class name.
	*@param string $class Class name of the singleton
	*@return mixed The instance of the singleton (or false)
	*/
	static public function GetInstance( $class )
	{
		if ( isset(BaseClassSingleton::$singleton[$class]) )
			return BaseClassSingleton::$singleton[$class];
		return false;
	}
	/**
	* This is basicly only used by the Atoms package to ensure proper destruction sequence.
	*@param string $class the singleton instance to free
	*/
	static public function DeleteInstance( $class )
	{
		unset( BaseClassSingleton::$singleton[$class] );
	}
}
?>